<?php
/**
 * This file is part of php-agenda.
 * 
 * php-agenda is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * php-agenda is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with php-agenda; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Copyright 2006, Thomas Abeel
 * 
 * Project: http://sourceforge.net/projects/php-agenda/
 * 
 */
?>
<?php
session_start();
include ("adodb/adodb.inc.php");
include ("local_config_inc.php");

if (isset ($_SESSION["id"]) && $_SESSION["id"] == 1) {
	//business logic
	$messages = array();
	if (isset ($_POST["newUser"])) {
		if (md5($_POST["pass1"]) == md5($_POST["pass2"])) {
			$db = ADONewConnection('mysql');
			$db->Connect($dbhost, $dbuser, $dbpass, $dbname);
			$db->debug = false;
			$userName = mysql_real_escape_string($_POST["username"]);
			$sql = "select * from users where display_name='$userName'";
			$recordSet = & $db->Execute($sql);
			$count = 0;
			while (!$recordSet->EOF) {
				$count++;
				$recordSet->MoveNext();

			}
			if ($count > 0) {
				array_push($messages,"The user already exists");
			} else {
				$sql = "insert into users (`display_name`,`md5pass`) values('$userName','" . md5($_POST["pass1"]) . "')";
				$db->Execute($sql) or array_push($messages,"Failed to insert user. -- $sql");
			}

			$db->Close();
		} else {
			array_push($messages,"The passwords should match");
		}
	}
	//----PAGE------
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
  <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
  <title>The Simple PHP Agenda</title>
  <LINK href="style.css.php" rel="stylesheet" type="text/css">
</head>
<body>
<?php


	$count=0;
		foreach ($messages as $line) {
			echo $line . '<br/>';
			$count++;
		}
		unset($messages);
		if ($count==0 and isset ($userName)) {
			echo 'User \'' . $userName . '\' succesfully added.';
		}

	
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST" >
<label for="username">Username: </label>
<input type="text" name="username" width=50/><br/>
<label for="pass1">Password: </label>
<input type="password" name="pass1" width=50/><br/>
<label for="pass2">Confirm pasword: </label>
<input type="password" name="pass2" width=50/><br/>
<input type="submit" name="newUser" value="Create new user"/>
</form>
</body>
</html>
<?php


} else {
	header("Location: index.php?today");
}
?>
